#include <string>
#include <X11/cursorfont.h>
#include "image.h"

int main() {
    char *dpy_name = getenv("DISPLAY");
    int screen;
    Display *dpy = XOpenDisplay(dpy_name);
    if (dpy == NULL) {
        printf("Cannot connect to X server\n");
        return -1;
    }

    screen = DefaultScreen(dpy);
    unsigned long white = WhitePixel(dpy, screen);
    Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 300, 300, 1, white, white);

    int width = 0, height = 0;
    std::string logo_path = "./pixmap/logo.png";
    XImage *ximage = load_png_image(dpy, screen, (char*)logo_path.c_str(), &width, &height);
    if (ximage == NULL) {
        XCloseDisplay(dpy);
        XDestroyWindow(dpy, w);
        return -1;
    }
    
    XMapWindow(dpy, w);
    XSetInputFocus(dpy, w, RevertToParent, CurrentTime);
    XSelectInput(dpy, w, KeyPressMask);
    while (true) {
        XEvent e;
        XPutImage(dpy, w, DefaultGC(dpy, screen), ximage, 0, 0, 0, 0, width, height);
        XDefineCursor(dpy, w, XCreateFontCursor(dpy, XC_top_left_arrow));
        XNextEvent(dpy, &e);
        if (e.type == KeyPress) {
            break;
        }
    }
	XDestroyImage(ximage);
    XCloseDisplay(dpy);
	XDestroyWindow(dpy, w);
    return 0;
}

